/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package model.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import model.entities.Customer;
import model.entities.Reservation;
import model.entities.RoleSV;
import model.entities.Room;
import model.entities.RoomType;


/**
 *
 * @author Hieu.Nguyen
 */
public class ReservationDAO {
    private static ReservationDAO reservationDAO;

    public static ReservationDAO getInstance() {
        if (reservationDAO == null) {
            reservationDAO = new ReservationDAO();
        }
        return reservationDAO;
    }
    
     public List<Reservation> getReservation(boolean isActived) {
        List<Reservation> listReservation = new ArrayList<Reservation>();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblReservation";
            //String sql = "select * from tblReservation, tblCustomer where tblReservation.CustomerID = tblCustomer.CustomerID";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            ResultSet rs = pstmt.executeQuery();
            CustomerDAO customer_ = new CustomerDAO();
            while (rs.next()) {
                Reservation r = new Reservation();
                
                r.setExpectedCheckinDate(rs.getDate("ExpectedCheckinDate"));
                r.setArrivalTime(rs.getString("ArrivalTime"));
                r.setExpectedCheckoutDate(rs.getDate("ExpectedCheckoutDate")); 
                r.setNumberGuests(rs.getInt("NumberGuests"));
                
                r.setCustomer(customer_.getCustomerByID(Integer.parseInt(rs.getString("CustomerID"))));
                
                //Customer cm = CustomerDAO.getInstance().getCustomerByID(rs.getInt("CustomerID"));
                
                Room aa = RoomDAO.getInstance().getRoomByID(rs.getInt("RoomID"));                
                r.setRoom(aa);
                
                r.setComments(rs.getString("Comments"));
                
               
                
                
                listReservation.add(r);
                
            }
            //JOptionPane.showMessageDialog(null, listReservation.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listReservation;
    }
     public Reservation getReservationByID(int reservationID) {

        Reservation rt = new Reservation();
        CustomerDAO rmd = new CustomerDAO();
        RoomDAO rd = new RoomDAO();
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "select * from tblReservation where ReservationID = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, reservationID);
            
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                
                rt.setReservationID(rs.getInt("ReservationID"));
                rt.setCustomer(rmd.getCustomerByID(Integer.parseInt(rs.getString("CustomerID"))));
                rt.setExpectedCheckinDate(rs.getDate("ExpectedCheckinDate"));
                rt.setArrivalTime(rs.getString("ArrivalTime"));
                rt.setExpectedCheckoutDate(rs.getDate("ExpectedCheckoutDate"));
                rt.setNumberGuests(rs.getInt("NumberGuests"));
                rt.setComments(rs.getString("Comments"));
                rt.setRoom(rd.getRoomByID(Integer.parseInt(rs.getString("RoomID"))));
                
                
                
                
             
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rt;
     }
    public boolean createReservation(Reservation reservation) {
        boolean isCheck = false;
        try {
            Connection conn = ConnectionFactory.getConnection();
            String sql = "insert into tblReservation values (?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
           
            pstmt.setInt(1, reservation.getReservationID());
            pstmt.setInt(2, reservation.getCustomer().getCustomerID());
            pstmt.setDate(3, reservation.getExpectedCheckinDate());
            pstmt.setString(4, reservation.getArrivalTime());
            pstmt.setDate(5, reservation.getExpectedCheckoutDate());
            pstmt.setInt(6, reservation.getNumberGuests());
            pstmt.setString(7, reservation.getComments());
            pstmt.setInt(8, reservation.getRoom().getRoomID());
            pstmt.executeUpdate();
            isCheck = true;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return isCheck;
     }
    
}
